{{define "user/add.html"}}
<!DOCTYPE html>
<html>
	<head>
		{{template "header"}}
	</head>

	<body>
		<div class="weadmin-nav">
            <span class="layui-breadcrumb">
                <a href="/welcome">首页</a>
                <a href="/admin/user/list">用户管理</a>
                <a><cite>添加用户</cite></a>
            </span>
		</div>
		<div class="weadmin-body">
			<form class="layui-form">
				<div class="layui-form-item">
					<div class="layui-inline">
						<label for="L_username" class="layui-form-label">
							<span class="we-red">*</span>用户名
						</label>
						<div class="layui-input-inline">
							<input type="text" id="L_username" name="user" lay-verify="username" autocomplete="off" class="layui-input" placeholder="用户名至少5个字符">
						</div>
					</div>

					<div class="layui-inline">
						<label for="L_orgid" class="layui-form-label">
							所属机构
						</label>
						<div class="layui-input-inline">
							<select name="orgid" lay-filter="orgid" id="L_orgid">
								<option value="0">请选择</option>
								{{ range .orgOptionList }}
								<option value="{{.Value}}">{{.Text}}</option>
								{{ end }}
							</select>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-inline">
						<label for="L_email" class="layui-form-label">
							<span class="we-red">*</span>手机
						</label>
						<div class="layui-input-inline">
							<input type="text" id="L_phone" name="phone" lay-verify="phone" autocomplete="" class="layui-input">
						</div>
					</div>

					<div class="layui-inline">
						<label for="L_state" class="layui-form-label">状态</label>
						<div class="layui-input-block" id="L_state">
							<input type="checkbox" name="state" id="L_state" lay-skin="switch"
								lay-text="启用|禁用" value="1" checked>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-inline">
						<label for="L_email" class="layui-form-label">
							邮箱
						</label>
						<div class="layui-input-inline">
							<input type="text" id="L_email" name="email" autocomplete="off" class="layui-input">
						</div>
					</div>

					<div class="layui-inline">
						<label for="L_gender" class="layui-form-label">性别</label>
						<div class="layui-input-block" id="L_gender">
							<input type="radio" name="gender" value="男" title="男" checked>
							<input type="radio" name="gender" value="女" title="女">
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-inline">
						<label for="L_pwd" class="layui-form-label">
							<span class="we-red">*</span>密码
						</label>
						<div class="layui-input-inline">
							<input type="password" id="L_pwd" name="pwd" lay-verify="pwd" autocomplete="off" class="layui-input" placeholder="6到16个字符">
						</div>
					</div>

					<div class="layui-inline">
						<label for="L_repass" class="layui-form-label">
							<span class="we-red">*</span>确认密码
						</label>
						<div class="layui-input-inline">
							<input type="password" id="L_repass" name="repass" lay-verify="repass" autocomplete="off" class="layui-input">
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-inline">
						<label for="L_role" class="layui-form-label">
							分配角色
						</label>
						<div class="layui-input-block">
							{{ range $k,$v := .roleList }}
								<label><input type="checkbox" class="roleid default-chk" name="roleid[]" value="{{$v.id}}"  title="{{$v.rolename}}"  lay-ignore>{{$v.rolename}}</label>&nbsp;&nbsp;
							{{ end }}
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<button class="layui-btn fr" lay-filter="add" lay-submit="">确定</button>
				</div>
			</form>
		</div>
    </body>

    {{template "jsfile"}}
	<script type="text/javascript">
		layui.extend({
			admin: '/static/js/admin',
		});
		layui.use(['jquery', 'form','element', 'admin', 'carousel'], function() {
			var element = layui.element,
				$ = layui.jquery,
				carousel = layui.carousel,
				form = layui.form,
				admin = layui.admin;
			//建造实例
			carousel.render({
				elem: '.weadmin-shortcut'
				,width: '100%' //设置容器宽度
				,arrow: 'none' //始终显示箭头
				,trigger: 'hover'
				,autoplay:false
			});

			form.verify({
				username: function(value){
					if(value.length < 5){
						return '用户名至少5个字符!';
					}
					if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
						return '用户名不能有特殊字符';
					}
					if(/(^\_)|(\__)|(\_+$)/.test(value)){
						return '用户名首尾不能出现下划线\'_\'';
					}
				},
				pwd: function(value){
					if(value.length < 6 || value.length > 16){
						return '密码不允许为空，6至16个字符!';
					}
				},
				repass: function(value){
					if(value != $("#L_pwd").val()){
						return '两次密码不相同!';
					}
				}
			})

			//监听提交
			form.on('submit(add)', function(data){
				var roleids = []
				$("input:checkbox.roleid:checked").each(function(){
					roleids.push(this.value);
				})
				data.field["roleids"]=roleids;
				$.post("/admin/user/add",data.field,function(result){
					if(result.status){
						layer.msg("添加用户成功!",{time:3*1000,icon:1},function(){
							//自动关闭layer
							var index = parent.layer.getFrameIndex(window.name);
							parent.layer.close(index);
							//刷新list
							parent.userRefreshForm();
						})
					}else{
						layer.msg(result.message,{time:6*1000,icon:2})
					}
				},"json");
				return false;
			});
		});
	</script>

</html>
{{end}}